/** cart 购物车操作 提供del、modify 以及 all方法*/
/**若用户未登录，购物车商品存储于本地,若用户已登录，则添加至server 
 * 本地商品保存sku所有信息，提取时，以本地信息展示
 * 存储结构为SKUID,SKU (将来若store要给其他模块使用，需要改写此类)
 * opt true用户操作.false系统操作
 * isSample 是否为品鉴装，若是，则为true
*/
function add(sku, quantity, opt, isSample){
  //用户未登录
  if (getApp().globalData.platUser == null) {
    addLocal(sku,quantity,true);
  }else{//用户为登录状态
    //添加动画
    wx.showLoading({
      title: '处理中...',
    })
    let sample = false;
    if (typeof (isSample) != 'undefined' && isSample){
      sample = true;
    }
    wx.request({
      url: getApp().globalData.host + 'cart/add',
      data: { skuId: sku.skuId, quantity: quantity, isSample: sample},
      header: getApp().globalData.header,
      success: function (res) {
        wx.hideLoading();
        if(opt){
          if(res.data.errorCode ==0 ){
            wx.showToast({
              title: "添加成功",
            })
          }else{
            wx.showModal({
              title: '添加失败',
              content:res.data.errorMsg,
              showCancel:false
            })
          }
        }
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  }
}

//更新购物车信息,若获取本地购物车商品信息失败，则直接save sku
function modify(sku,quantity){
  //用户未登录，操作同addLocal
  if (getApp().globalData.platUser == null) {
    addLocal(sku, quantity,false);
  }else{
    sku.quantity = sku.quantity + quantity;
    if (sku.quantity > sku.actualWholesaleMaxQuantity) sku.quantity = sku.actualWholesaleMaxQuantity;
    wx.request({
      url: getApp().globalData.host + 'cart/setQty',
      data: { skuId: sku.skuId, qty: sku.quantity, sysNo: sku.sysNo },
      header: getApp().globalData.header,
      success: function (res) {//不管更新成功与否，不做任何操作
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  }
}

/** 全选/全取消 */
function batchSelect(status){
  if (getApp().globalData.platUser == null) {//用户未登录
    changeStatus(status);
  } else {
    var url = "cart/uncheckAll";
    if (status)
      url = "cart/checkAll";
    wx.request({
      url: getApp().globalData.host + url,
      header: getApp().globalData.header,
      success: function (res) { },//不管更新成功与否，不做任何操作
      fail: function (res) { },
      complete: function (res) { }
    })
  }
}

/** 选中/取消选中 */
function singleSelect(sku,status) {
  if (getApp().globalData.platUser == null) {//用户未登录
    try {
      var oldsku = wx.getStorageSync(sku.skuId);
      oldsku.isChecked = status;
      wx.setStorage({
        key: sku.skuId,
        data: oldsku,
      })
    } catch (e) {}
  } else {
    var url = "cart/uncheckSingle";
    if (status)
      url = "cart/checkSingle";
    wx.request({
      url: getApp().globalData.host + url,
      data: {sysNo: sku.sysNo},
      header: getApp().globalData.header,
      success: function (res) { },//不管更新成功与否，不做任何操作
      fail: function (res) { },
      complete: function (res) { }
    })
  }
}

/** 变更本地状态 */
function changeStatus(status){
  try {
    var res = wx.getStorageInfoSync();
    for (var i = 0; i < res.keys.length; i++) {
      try {
        var sku = wx.getStorageSync(res.keys[i]);
        sku.isChecked = status;
        wx.setStorage({
          key: sku.skuId,
          data: sku,
        })
      } catch (e) { }
    }
  } catch (e) {}
}

/** 清除购物车商品，若用户未登录，清除本地商品信息，若用户已登录，则清除服务器商品信息 */
function del(sku){
  //用户未登录
  if (getApp().globalData.platUser == null) {
    try {
      wx.removeStorage({
        key: sku.skuId,
        success: function(res) {},
      })
    }catch(e){}
  } else {//用户为登录状态
    wx.request({
      url: getApp().globalData.host + 'cart/delete',
      data: {sysNo: sku.sysNo },
      header: getApp().globalData.header,
      success: function (res) {//不管删除成功与否，不做任何操作
      },
      fail: function (res) { },
      complete: function (res) { }
    })
  }
}

/** 若用户注册成功，则将本地所有商品数据add到server，并清除本地数据 */
function register(){
  if (getApp().globalData.platUser == null)
    return;
  try {
    var res = wx.getStorageInfoSync();
    if (res.keys.length<1)return;
    for (var i = 0; i < res.keys.length;i++){
      try{
        wx.getStorage({
          key: res.keys[i],
          success: function(res) {
            add(res.data, res.data.quantity,false);
          } 
        })

        //var sku = wx.getStorageSync(res.keys[i]);
        //add(sku, sku.quantity,false);
      }catch(e){}
    }
  }catch(e){}
  finally {
    //不论成功与否，清除本地缓存
    wx.clearStorage();
  }
}

//添加到本地购物车
function addLocal(sku,quantity,opt){
  try {
    var oldsku = wx.getStorageSync(sku.skuId);
    if (oldsku)
      sku.quantity = oldsku.quantity + quantity;
    else
      sku.quantity = quantity;
    wx.setStorage({
      key: sku.skuId,
      data: sku,
    })
  } catch (e) {
    sku.quantity = quantity;
    wx.setStorage({
      key: sku.skuId,
      data: sku,
    })
  }
  if(opt){
	 wx.showToast({
	  title: "添加成功",
	 })
  }
}

module.exports = {
  add: add,
  modify: modify,
  del: del,
  register: register,
  batchSelect: batchSelect,
  singleSelect: singleSelect
}